package com.wcity.manage.web.module;

import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.ModuleMapper;
import com.wcity.manage.dao.model.Module;
import com.wcity.manage.service.ModuleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by jn on 2016/12/8.
 * 后台模块ctrl
 */
@Controller
public class ModuleAct {

    /**
     * 修改模块
     * @param module
     * @param response
     */
    @RequestMapping("module_edit")
    public void moduleEdit(Module module, HttpServletResponse response){

        String errorMsg = moduleService.editModule( module );

        if ( StringUtil.isBlank( errorMsg ) ) {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "修改成功", "module_list", "", "closeCurrent", "" ) );
        } else {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }

    }

    /**
     * 模块修改跳转
     * @return
     */
    @RequestMapping("to_module_edit")
    public ModelAndView toModuleEdit(String id){
        Module module = moduleMapper.find( Integer.parseInt( id ) );
        List<Module> list = moduleMapper.findParent();
        ModelAndView mav = new ModelAndView("module/moduleedit");
        mav.addObject("module",module);
        mav.addObject("list",list);
        return mav;
    }

    /**
     * 新增模块
     * @param module
     * @param response
     */
    @RequestMapping("module_add")
    public void moduleAdd(Module module, HttpServletResponse response){

        String errorMsg = moduleService.addModule( module );

        if ( StringUtil.isBlank( errorMsg ) ) {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "添加成功", "module_list", "", "closeCurrent", "" ) );
        } else {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }

    }

    /**
     * 新增模块跳转
     * @return
     */
    @RequestMapping("to_module_add")
    public ModelAndView toModuleAdd(){
        List<Module> list = moduleMapper.findParent();
        ModelAndView mav = new ModelAndView("module/moduleadd");
        mav.addObject("list",list);
        return mav;
    }

    /**
     * 模块列表
     * @param pageModel
     * @return
     */
    @RequestMapping("module_list")
    public ModelAndView moduleList(PageModel<Module> pageModel,Module module){

        pageModel.setParams( module );
        List<Module> list = moduleMapper.findListPage( pageModel );

        ModelAndView mav = new ModelAndView("module/modulelist");
        mav.addObject("list",list);
        mav.addObject("pageModel",pageModel);
        mav.addObject("module",module);
        return mav;
    }

    @Resource private ModuleService moduleService;
    @Resource private ModuleMapper moduleMapper;

}
